package hotel.base.oversea.utils;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 流关闭
 */
public class StreamUtils {

	public static void close(InputStream inputStream) {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
			}
		}
	}

	public static void close(InputStream... inputStreams) {
		if (inputStreams != null && inputStreams.length > 0) {
			for (InputStream is : inputStreams) {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public static void close(InputStream inputStream, OutputStream... outputStreams) {
		try {
			if (inputStream != null)
				inputStream.close();
		} catch (IOException e) {
		}
		if (outputStreams != null && outputStreams.length > 0) {
			for (OutputStream os : outputStreams) {
				if (os != null) {
					try {
						os.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public static void close(OutputStream outputStream) {
		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
			}
		}
	}

	public static void close(OutputStream... outputStreams) {
		if (outputStreams != null && outputStreams.length > 0) {
			for (OutputStream os : outputStreams) {
				if (os != null) {
					try {
						os.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public static void close(OutputStream outputStream, InputStream... inputStreams) {
		if (inputStreams != null && inputStreams.length > 0) {
			for (InputStream is : inputStreams) {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
		}
		try {
			if (outputStream != null)
				outputStream.close();
		} catch (IOException e) {
		}
	}

	public static void close(InputStream inputStream, OutputStream outputStream) {
		try {
			if (inputStream != null)
				inputStream.close();
			if (outputStream != null)
				outputStream.close();
		} catch (IOException e) {
		}
	}

	public static void close(AutoCloseable... closeables) {
		if (closeables != null && closeables.length > 0) {
			for (AutoCloseable ac : closeables) {
				if (ac != null) {
					try {
						ac.close();
					} catch (Exception e) {
					}
				}
			}
		}
	}

	/**
	 * @Author lmf
	 * @Date 2020/5/15 16:38
	 **/
	public static class BeanUtil {
		/**
		 * 大小写可以忽略
		 * 下划线 _ 被忽略
		 * NULL值和空字符串不会覆盖新值
		 *
		 * @param source
		 * @param target
		 * @param <T>
		 * @return
		 */
		public static <T> T copyPropertiesIgnoreCase(Object source, Object target) {
			Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
			CacheFieldMap.getFieldMap(target.getClass()).values().forEach((it) -> {
				Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
				if (field != null) {
					it.setAccessible(true);
					field.setAccessible(true);
					try {
						//忽略null和空字符串
						if(field.get(source)!=null&& StringUtils.isBlank(field.get(source).toString()))
							it.set(target, field.get(source));
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			});
			System.out.println(target.toString());
			return (T) target;
		}
		private static class CacheFieldMap {
			private static Map<String, Map<String, Field>> cacheMap = new HashMap<>();

			private static Map<String, Field> getFieldMap(Class clazz) {
				Map<String, Field> result = cacheMap.get(clazz.getName());
				if (result == null) {
					synchronized (CacheFieldMap.class) {
						if (result == null) {
							Map<String, Field> fieldMap = new HashMap<>();
							for (Field field : clazz.getDeclaredFields()) {
								fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
							}
							cacheMap.put(clazz.getName(), fieldMap);
							result = cacheMap.get(clazz.getName());
						}
					}
				}
				return result;
			}
		}
	}
}
